package com.kc.web.controller.business;

import cn.hutool.core.util.ArrayUtil;
import com.kc.business.domain.BizCreation;
import com.kc.business.domain.BizImageSize;
import com.kc.business.domain.BizScenic;
import com.kc.business.domain.BizUserCreation;
import com.kc.business.service.IBizCreationService;
import com.kc.business.service.IBizGoodsService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import com.kc.common.utils.ImagesUtils;
import com.kc.common.utils.JsonUtils;
import com.kc.common.utils.StringUtils;
import com.kc.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 用户创作相关
 */
@RestController
@RequestMapping("/creation")
@Slf4j
public class BizCreationController extends BaseController {
    @Autowired
    private IBizCreationService iBizCreationService;
    @Autowired
    private ISysDictDataService iSysDictDataService;
    @Autowired
    private IBizGoodsService bizGoodsService;
    /**
     * 用户创作列表
     * @param bizUserCreation
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:creation:getList')")
    @GetMapping(value = "/getList")
    public TableDataInfo getList(BizUserCreation bizUserCreation){
        startPage();
        List<BizCreation> userCreationList = iBizCreationService.getList(bizUserCreation);
        //获取分类名称
        for(BizCreation userCreation:userCreationList){
            userCreation.setTypeName(iSysDictDataService.selectDictLabel("user_creation_type",userCreation.getType().toString()));
            userCreation.setImageList(StringUtils.split(userCreation.getImage(),","));
            //处理推荐商品列表
            if(StringUtils.isNotEmpty(userCreation.getGoodsId())){
                //
                String[] ids = StringUtils.split(userCreation.getGoodsId(),",");
                userCreation.setGoodsIds(ids);
                userCreation.setGoodsRecommendListDTOS(bizGoodsService.getGoodsRecommendList(ids));

            }
        }
        return getDataTable(userCreationList);
    }

    /**
     * 新增数据
     * @param bizCreation
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:creation:add')")
    @Log(title = "旅知新增", businessType = BusinessType.INSERT)
    @PostMapping(value = "/setCreation")
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody BizCreation bizCreation) {
        //图片数组转字符串逗号分隔
        //图片拆分成图片list
        //List<String> imageList = Arrays.asList(StringUtils.split(bizCreation.getImage(),","));
        List<String> imageList = Arrays.asList(bizCreation.getImageList());
        //获取图片的宽高
        List<BizImageSize> imageSizeDTOList = new ArrayList<>();
        imageList.forEach(image->{
            try {
                Map<String,Integer> map = ImagesUtils.getImgSize(image);
                imageSizeDTOList.add(new BizImageSize(image,map.get("width"),map.get("height")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        bizCreation.setImgageAttr(JsonUtils.toJson(imageSizeDTOList));
        bizCreation.setImage(StringUtils.join(bizCreation.getImageList(), ","));
        //处理推荐商品
        if(ArrayUtil.isNotEmpty(bizCreation.getGoodsIds())){
            bizCreation.setGoodsId(StringUtils.join(bizCreation.getGoodsIds(), ","));
        }
        return toAjax(iBizCreationService.insertData(bizCreation));
    }

    /**
     * 修改数据
     * @param bizCreation
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:creation:edit')")
    @Log(title = "旅知修改", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/setCreation")
    @RepeatSubmit
    public AjaxResult update(@Validated @RequestBody BizCreation bizCreation) {
        if(bizCreation.getId() == 0 || bizCreation.getId()== null){
            AjaxResult.error("参数错误");
            return toAjax(0);
        }
        List<String> imageList = Arrays.asList(bizCreation.getImageList());
        //获取图片的宽高
        List<BizImageSize> imageSizeDTOList = new ArrayList<>();
        imageList.forEach(image->{
            try {
                Map<String,Integer> map = ImagesUtils.getImgSize(image);
                imageSizeDTOList.add(new BizImageSize(image,map.get("width"),map.get("height")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        bizCreation.setImgageAttr(JsonUtils.toJson(imageSizeDTOList));
        bizCreation.setImage(StringUtils.join(bizCreation.getImageList(), ","));
        //处理推荐商品
        if(ArrayUtil.isNotEmpty(bizCreation.getGoodsIds())){
            bizCreation.setGoodsId(StringUtils.join(bizCreation.getGoodsIds(), ","));
        }
        return toAjax(iBizCreationService.updateData(bizCreation));
    }

    /**
     * 删除数据
     */
    @PreAuthorize("@ss.hasPermi('business:creation:remove')")
    @Log(title = "旅知数据管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Integer id) {
        BizCreation bizCreation = new BizCreation();
        bizCreation.setId(id);
        bizCreation.setIsDeleted(1);
        return toAjax(iBizCreationService.deleteData(bizCreation));
    }

    /**
     * 获取推荐商品列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:creation:recommentGoodsList')")
    @GetMapping(value = "/recommentGoodsList")
    public TableDataInfo recommentGoodsList(){
//        Integer[] ids = {935,938};
//        return getDataTable(bizGoodsService.getGoodsRecommendList(ids));
        return getDataTable(bizGoodsService.getGoodsRecommendList(null));
    }



}
